%{
#include "absyn.h"
#include "grammer.tab.h"
#include <string.h>
#include "errormsg.h"

int charPos = 1;

int yywrap()
{
    return 1;
};

void adjust()
{
    EM_tokPos = charPos;
    charPos += yyleng;
};

%}
%x ASMMODE1 ASMMODE2

char     [a-zA-Z]+
num    [0-9]+
blank    [ \t]

%%
__asm    { adjust(); BEGIN ASMMODE1; return ASM;}
<ASMMODE1>"{\n"    { adjust(); EM_newline();
            BEGIN ASMMODE2; return LPAREN; }
<ASMMODE1>{blank}+ {adjust(); continue;}
<ASMMODE1>\n    { adjust(); EM_newline(); continue; }

<ASMMODE2>^{blank}+ { adjust(); continue;}
<ASMMODE2>[^ \t\n}][^\n}]+ { adjust(); yylval.sval = yytext;
            return ASM_CODE; }
<ASMMODE2>\n    { adjust(); EM_newline(); return ASM_NL;}
<ASMMODE2>"}"    { adjust(); BEGIN INITIAL; return RPAREN; }

\n    { adjust(); EM_newline(); continue;}
\/\/.*    { adjust(); continue;}
^#.*    { adjust(); continue;}
{blank}+ { adjust(); continue;}

,    { adjust(); return COMMA;}
:    { adjust(); return COLON;}
;    { adjust(); return SEMICOLON;}
"("    { adjust(); return LBRACK;}
")"    { adjust(); return RBRACK;}
"{"    { adjust(); return LPAREN;}
"}"    { adjust(); return RPAREN;}
\[    { adjust(); return LBRACE; }
\]    { adjust(); return RBRACE;}

"++"    { adjust(); return SELFADD;}
\.    { adjust(); return DOT;}
"+"    { adjust(); return PLUS;}
"-"    { adjust(); return MINUS;}
"*"    { adjust(); return TIMES;}
\/    { adjust(); return DIVIDE;}
==    { adjust(); return EQ;}
!=    { adjust(); return NEQ;}
"<"    { adjust(); return LT;}
"<="    { adjust(); return LE;}
\>    { adjust(); return GT;}
>=    { adjust(); return GE;}
&&    { adjust(); return AND;}
"||"    { adjust(); return OR;}
=    { adjust(); return ASSIGN;}
"<<"    { adjust(); return LSHIFT;}
">>"    { adjust(); return RSHIFT;}
"|>"    { adjust(); return ARSHIFT;}
"|"        { adjust(); return XOR;}

while    { adjust(); return WHILE; }
for    { adjust(); return FOR; }
if    { adjust(); return IF; }
else    { adjust(); return ELSE; }
break    { adjust(); return BREAK; }
typedef    { adjust(); return TYPEDEF; }
struct    { adjust(); return STRUCT; }

{char}[{char}_0-9]*     { adjust(); yylval.sval = String(yytext); return ID;}
\"[^"\n]*\"     { adjust(); yylval.sval = String(yytext); return STRING; }
({num}+{num}*)         { adjust(); yylval.ival = atoi(yytext); return INT; }
